package gui;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;

import core.GameFlow;
import core.rendering.RenderableMouse;

public class Menu extends RenderableMouse {

	Vector<MenuItem> menuItems;
	Iterator<MenuItem> it;
	Iterator<MenuItem> it2;
	Vector<GameButtonListener> listeners;

	public Menu() {
		super();
		this.setSize(new Dimension(GameFlow.getInstance().getWidth(), GameFlow
				.getInstance().getHeight()));
		menuItems = new Vector();
		listeners = new Vector<GameButtonListener>();
	}

	@Override
	public void renderComponent(Graphics2D g2d) {
		// TODO Auto-generated method stub
		/**
		 * Renders now the elements and MenuItems-.
		 */
		
		it = menuItems.iterator();

		while (it.hasNext()) {
			it.next().render(g2d);
		}
		
	}

	@Override
	public boolean onClick(MouseEvent e) {
		// TODO Auto-generated method stub
		boolean retorno = false;
		it = menuItems.iterator();
		while (it.hasNext()) {
			retorno = it.next().onClick(e);
			if (retorno) {
				return true;
			}
		}
		return retorno;
	}

	@Override
	public void updateMouse(Point p) {

		// TODO Auto-generated method stub
		it2 = menuItems.iterator();
		while (it2.hasNext()) {
			it2.next().updateMouse(p);
		}
	}

	@Override
	public void updateLogic() {
		// TODO Auto-generated method stub

	}

	public void addButtonListener(GameButtonListener l) {
		listeners.add(l);
	}

	public void addButton(String button_label, int button_xpos,
			int button_ypos, int button_width, int button_height,
			int button_function) {
		GameButton button = this.createButton(button_label, button_xpos,
				button_ypos, button_width, button_height, button_function);
		Iterator<GameButtonListener> it = listeners.iterator();
		while (it.hasNext()) {
			GameButtonListener l = it.next();
			button.addGameButtonListener(l);
		}
		menuItems.add(button);
	}

	public void addLabel(String label, int xpos, int ypos, int width, int height) {
		GameTextLabel tl = new GameTextLabel(label, xpos, ypos, width, height);
		menuItems.add(tl);
	}

	public void addCashLabel(int xpos, int ypos, int width, int height) {
		GameTextLabelPlayerCash tl = new GameTextLabelPlayerCash(xpos, ypos,
				width, height);
		menuItems.add(tl);
	}

	public void addLivesLabel(int xpos, int ypos, int width, int height) {
		GameTextLabelPlayerLives tl = new GameTextLabelPlayerLives(xpos, ypos,
				width, height);
		menuItems.add(tl);
	}

	public GameButton createButton(String button_label, int button_xpos,
			int button_ypos, int button_width, int button_height,
			int button_function) {
		GameButton button = new GameButton(button_label, button_width,
				button_height);
		button.setPosition(button_xpos, button_ypos);
		button.addFunction(button_function);
		return button;
	}

	/**
	 * this should be implemented in a different way.
	 * 
	 * @param xcells
	 * @param ycells
	 */
	public void addTowerSelectionPanel(int xpos, int ypos, int xcells,
			int ycells) {
		int a[][] = new int[xcells][ycells];
		int val=0;
		for (int cont = 0 ; cont < a.length ; cont++){
			for (int cont2 = 0 ; cont2 < a[cont].length ; cont2++){
				a[cont][cont2] = val++;
			}
		}
		TowerSelectionButtonGroup tg = new TowerSelectionButtonGroup(a, xpos,
				ypos);

		menuItems.add(tg);
	}
	
	public void addMenuItem(MenuItem item){
		menuItems.add(item);
	}

}
